package cn.skyui.animator.photo;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.windward.vii.library.glide.GlideApp;
import com.windward.vii.library.utils.ScreenUtils;
import com.windward.vii.library.utils.ToastUtils;

import java.util.ArrayList;

import cn.skyui.animator.R;

public class PhotoActivity extends AppCompatActivity {

    private ArrayList<Photo> photoList;
    private int mCurrentPosition = 0;

    private ViewPager mViewPager;
    private PhotoPageAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);

        //延迟动画
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            postponeEnterTransition();
        }

        mCurrentPosition = getIntent().getIntExtra("position", 0);
        photoList = (ArrayList<Photo>) getIntent().getSerializableExtra("photoList");
        if(photoList == null || photoList.size() == 0) {
            ToastUtils.showShort("图片列表为空");
            return;
        }

        initView();
    }


    private void initView() {
        mViewPager = findViewById(R.id.viewPager);
        mAdapter = new PhotoPageAdapter(this, photoList);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mCurrentPosition, false);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        supportFinishAfterTransition();
    }


    public class PhotoPageAdapter extends PagerAdapter {

        private Activity mActivity;
        private ArrayList<Photo> images;

        public PhotoPageAdapter(Activity activity, ArrayList<Photo> images) {
            this.mActivity = activity;
            this.images = images;
        }

        public void setData(ArrayList<Photo> images) {
            this.images = images;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView photoView = new ImageView(mActivity);
            Photo photo = images.get(position);
            Log.i("Feeds","transitionName = " + photo.id + "-" + photo.index);
            ViewCompat.setTransitionName(photoView, photo.id + "-" + photo.index);
            GlideApp.with(mActivity)
                    .load(photo.url)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            supportStartPostponedEnterTransition();
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            supportStartPostponedEnterTransition();
                            return false;
                        }
                    })
                    .into(photoView);
//            Glide.with(mActivity)
//                    .load(photo.url)
//                    .apply(new RequestOptions()
//                            .placeholder(R.drawable.ic_default_image)
//                            .error(R.drawable.ic_default_image)
//                            .override(ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight()) //指定图片大小(像素)
//                            .diskCacheStrategy(DiskCacheStrategy.ALL))
//                    .into(photoView);
            photoView.setOnClickListener(v -> {
                supportFinishAfterTransition();
            });
            container.addView(photoView);
            return photoView;
        }

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }


}
